﻿using AutoMapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.Useres;
using MediatR;
using Microsoft.Extensions.Logging;
using MySqlX.XDevAPI.Common;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.Useres
{
    public class DelUserCommandHandler : IRequestHandler<DelUserCommand, ApiResult<int>>
    {
        IBaseRepository<Users> Users;
        IMapper mapper;

        public DelUserCommandHandler(IBaseRepository<Users> Users, IMapper mapper)
        {
            this.Users = Users;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(DelUserCommand request, CancellationToken cancellationToken)
        {   ApiResult<int> result = new ApiResult<int>();
            try
            {
             
                var depot = Users.GetQuery().Where(x => x.UId == request.UserId).First();
                depot.IsDel = true;
                var Ccout = Users.UpdateAsync(depot).Result;
                if (Ccout > 0)
                {
                    result.Message = "删除成功";
                    result.Data = Ccout;
                    result.Code = ApiEnum.success;
                }
                else
                {
                    result.Message = "未发现";
                    result.Data = Ccout;
                    result.Code = ApiEnum.error;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = ApiEnum.error;
            }
            return Task.FromResult(result);
        }
    }
}
